package com.leyou.item.service.iml;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryBrandMapper;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryBrandServiceimpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Autowired
    CategoryService categoryService;
    @Autowired
    private BrandService brandService;

    @Override
    public List<CategoryDTO> queryCategorybybrandid(Long bid) {
        //select * from tb-category where from in(select category_id where brand_id=#{cid})
        //根据bid获取集合CategoryBrand数据 此时对象是CategoryBrand
        List<CategoryBrand> categoryBrands = query().eq("brand_id", bid).list();
        //将对象CategoryBrand转化成只有数字的集合
        List<Long> cids = categoryBrands.stream().map(CategoryBrand::getCategory_id).collect(Collectors.toList());
        //利用方法查询
        List<Category> categories = categoryService.listByIds(cids);
        return CategoryDTO.convertEntityList(categories);


    }

    @Override
    public List<BrandDTO> queryBrandByCategory(Long cid) {
        //select * from tb_category where id in(select brand_id where category_id = #{category_id})

        //根据分类 的id查询对应的品牌id集合
        List<Long> brandIds = query().eq("category_id", cid).list()
                .stream()
                .map(CategoryBrand::getBrand_id)
                .collect(Collectors.toList());

        return BrandDTO.convertEntityList(this.brandService.listByIds(brandIds));
    }



}

